package com.itheima.examination5;

import java.util.Comparator;
import java.util.TreeSet;

public class StudentTest {
    public static void main(String[] args) {
        // 创建TreeSet集合，用于存储Student对象
        // 按照总分从高到低排序，总分相同时按物理成绩从高到低排序
        TreeSet<Student> studentSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // 主要条件：按总分从高到低排序
                int totalScoreCompare = Integer.compare(s2.getTotalScore(), s1.getTotalScore());
                if (totalScoreCompare != 0) {
                    return totalScoreCompare;
                }
                // 次要条件：总分相同时按物理成绩从高到低排序
                return Integer.compare(s2.getPhysic(), s1.getPhysic());
            }
        });

        // 实例化5个Student对象
        Student student1 = new Student("爱迪生", 90, 55);
        Student student2 = new Student("牛顿", 100, 90);
        Student student3 = new Student("拉瓦锡", 70, 100);
        Student student4 = new Student("霍金", 100, 100);
        Student student5 = new Student("伽利略", 60, 85);
        Student student6 = new Student("达尔文", 70, 75);

        // 将对象添加到集合
        studentSet.add(student1);
        studentSet.add(student2);
        studentSet.add(student3);
        studentSet.add(student4);
        studentSet.add(student5);
        studentSet.add(student6);

        // 遍历集合打印所有元素信息
        System.out.println("学生信息排序结果（按总分从高到低，总分相同按物理成绩从高到低）：");
        System.out.println("姓名\t\t物理\t化学\t总分");
        System.out.println("--------------------------------");
        for (Student student : studentSet) {
            System.out.println(student.getName() + "\t\t" +
                    student.getPhysic() + "\t" +
                    student.getChemistry() + "\t" +
                    student.getTotalScore());
        }

        System.out.println("\n详细信息：");
        for (Student student : studentSet) {
            System.out.println(student);
        }
    }
}
